package com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.service.impl;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.unsafeevent.baqsjcommon.BaqsjCOMMON;
import com.fufan.mtsaas.modules.unsafeevent.baqsjcommon.BaqsjEventCOMMON;
import com.fufan.mtsaas.modules.unsafeevent.baqsjcommon.EventClassBo;
import com.fufan.mtsaas.modules.unsafeevent.baqsjcommon.EventClassListBo;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordaccident.bo.request.*;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordaccident.dao.BaqsjRecordAccidentDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordaccident.model.BaqsjRecordAccident;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordaccidentother.dao.BaqsjRecordAccidentOtherDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordaccidentother.model.BaqsjRecordAccidentOther;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordhandle.dao.BaqsjRecordHandleDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordhandle.model.BaqsjRecordHandle;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordinputblood.dao.BaqsjRecordInputbloodDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordinputblood.model.BaqsjRecordInputblood;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.bo.request.*;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.bo.response.*;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.dao.BaqsjRecordDetailDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.dao.BaqsjRecordMainDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.model.BaqsjRecordDetail;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.model.BaqsjRecordMain;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmedicalinstrument.dao.BaqsjRecordMedicalinstrumentDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmedicalinstrument.model.BaqsjRecordMedicalinstrument;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordnegotiation.dao.BaqsjRecordNegotiationDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordnegotiation.model.BaqsjRecordNegotiation;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordoperation.bo.request.BaqsjRecordNonPlaned;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordoperation.bo.request.BaqsjRecordOperationBad;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordoperation.bo.request.BaqsjRecordSurgicalBad;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordoperation.dao.BaqsjRecordOperationDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordoperation.model.BaqsjRecordOperation;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordother.bo.request.BaqsjRecordOtherPublic;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordother.bo.request.BaqsjRecordOtherPublicSecurity;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordother.dao.BaqsjRecordOtherDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordother.model.BaqsjRecordOther;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordproperty.dao.BaqsjRecordPropertyDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordproperty.model.BaqsjRecordProperty;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordprotection.dao.BaqsjRecordProtectionDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordprotection.model.BaqsjRecordProtection;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordreport.dao.BaqsjRecordReportDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordreport.model.BaqsjRecordReport;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordtransfusion.dao.BaqsjRecordTransfusionDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordtransfusion.model.BaqsjRecordTransfusion;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecorduntowardeffect.dao.BaqsjRecordUntowardeffectDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecorduntowardeffect.model.BaqsjRecordUntowardeffect;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordwrongblood.dao.BaqsjRecordWrongbloodDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordwrongblood.model.BaqsjRecordWrongblood;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordwrongdrug.dao.BaqsjRecordWrongdrugDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordwrongdrug.model.BaqsjRecordWrongdrug;
import com.fufan.mtsaas.util.DateUtil;
import com.fufan.mtsaas.util.JSonUtil;
import com.fufan.mtsaas.util.ParamUtil;
import com.fufan.mtsaas.util.SetGetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.function.Consumer;

@Service("BaqsjRecordEventQueryProcessSvcImpl")
@Slf4j
@Primary
public class BaqsjRecordEventQueryProcess implements BaqsjRecordBaseProcess {
    @Autowired
    private ParamUtil paramUtil;
    @Autowired
    private BaqsjRecordMainDao baqsjRecordMainDao;
    @Autowired
    private BaqsjRecordDetailDao baqsjRecordDetailDao;
    @Autowired
    private BaqsjRecordPropertyDao baqsjRecordPropertyDao;
    @Autowired
    private BaqsjRecordReportDao baqsjRecordReportDao;
    @Autowired
    private BaqsjRecordHandleDao baqsjRecordHandleDao;
    @Autowired
    private BaqsjRecordAccidentDao baqsjRecordAccidentDao;
    @Autowired
    private BaqsjRecordAccidentOtherDao baqsjRecordAccidentOtherDao;
    @Autowired
    private BaqsjRecordInputbloodDao baqsjRecordInputbloodDao;
    @Autowired
    private BaqsjRecordMedicalinstrumentDao baqsjRecordMedicalinstrumentDao;
    @Autowired
    private BaqsjRecordNegotiationDao baqsjRecordNegotiationDao;
    @Autowired
    private BaqsjRecordOperationDao baqsjRecordOperationDao;
    @Autowired
    private BaqsjRecordOtherDao baqsjRecordOtherDao;
    @Autowired
    private BaqsjRecordProtectionDao baqsjRecordProtectionDao;
    @Autowired
    private BaqsjRecordTransfusionDao baqsjRecordTransfusionDao;
    @Autowired
    private BaqsjRecordUntowardeffectDao baqsjRecordUntowardeffectDao;
    @Autowired
    private BaqsjRecordWrongbloodDao baqsjRecordWrongbloodDao;
    @Autowired
    private BaqsjRecordWrongdrugDao baqsjRecordWrongdrugDao;

    private Map<Class<?>, Object> daoMap = new HashMap<>();
    private Map<String, Class<?>> classMap = new HashMap<>();

    public BaqsjRecordEventQueryProcess() {

        classMap = new HashMap<>();
        classMap.put("BaqsjRecordProperty", BaqsjRecordProperty.class);
        classMap.put("BaqsjRecordReport", BaqsjRecordReport.class);
        classMap.put("BaqsjRecordHandle", BaqsjRecordHandle.class);
        classMap.put("BaqsjRecordAccidentChokingFood", BaqsjRecordAccident.class);
        classMap.put("BaqsjRecordAccidentFall", BaqsjRecordAccident.class);
        classMap.put("BaqsjRecordAccidentHurt", BaqsjRecordAccidentHurt.class);
        classMap.put("BaqsjRecordAccidentOther", BaqsjRecordAccidentOther.class);
        classMap.put("BaqsjRecordAccidentOut", BaqsjRecordAccidentOut.class);
        classMap.put("BaqsjRecordAccidentSelfKill", BaqsjRecordAccidentOther.class);
        classMap.put("BaqsjRecordDiagnosisDietWrong", Object.class);
        classMap.put("BaqsjRecordDiagnosisInfusionReaction", Object.class);
        classMap.put("BaqsjRecordDiagnosisMedicalHandle", Object.class);
        classMap.put("BaqsjRecordDiagnosisOtherReaction", Object.class);
        classMap.put("BaqsjRecordDiagnosisSampleWrong", Object.class);
        classMap.put("BaqsjRecordDoctorAndPatientNegotiation", BaqsjRecordNegotiation.class);
        classMap.put("BaqsjRecordHospitalInfected", Object.class);
        classMap.put("BaqsjRecordMedicalBadReaction", BaqsjRecordUntowardeffect.class);
        classMap.put("BaqsjRecordMedicalWrongDrug", BaqsjRecordWrongdrug.class);
        classMap.put("BaqsjRecordMedicalInstrument", BaqsjRecordMedicalinstrument.class);
        classMap.put("BaqsjRecordOtherReactionGeneral", Object.class);
        classMap.put("BaqsjRecordOtherReactionPublic", BaqsjRecordOtherPublic.class);
        classMap.put("BaqsjRecordOtherReactionPublicSecurity", BaqsjRecordOtherPublicSecurity.class);
        classMap.put("BaqsjRecordPreventionAndProtection", BaqsjRecordProtection.class);
        classMap.put("BaqsjRecordSurgicalBad", BaqsjRecordSurgicalBad.class);
        classMap.put("BaqsjRecordSurgicalNonPlaned", BaqsjRecordNonPlaned.class);
        classMap.put("BaqsjRecordSurgicalOperationBad", BaqsjRecordOperationBad.class);
        classMap.put("BaqsjRecordTransfusionBloodReaction", BaqsjRecordTransfusion.class);
        classMap.put("BaqsjRecordTransfusionWrongBlood", BaqsjRecordWrongblood.class);

    }

    /**
     * @param eventNo
     * @param dataRequest
     * @param requestPojo
     * @return
     */
    @Override
    public Result processEventData(String eventNo, Object dataRequest, RequestPojo requestPojo) {
        Result result = new Result();
        Page page = new Page();

        String eventList = paramUtil.getTSysParam("BAQSJ_EVENT");
        List<EventClassListBo> eventClassListBoList = null;
        ObjectMapper mapper = new ObjectMapper();
        try {
            eventClassListBoList = mapper.readValue(eventList, new TypeReference<List<EventClassListBo>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
            result.setResStatus(ResStatus.STATUS_FAILED);
            result.setResMsg(ResStatus.STATUS_FAILED_MSG + ":" + e.getLocalizedMessage());
            return result;
        }

        Long sysId = (Long) SetGetUtil.getField(dataRequest, "id");

        BaqsjRecordMain main = baqsjRecordMainDao.queryById(sysId);
        if (Objects.isNull(main)) {
            result.setResStatus(ResStatus.STATUS_UNAVAILABLE);
            result.setResMsg(ResStatus.STATUS_UNAVAILABLE_MSG + "：id=" + String.valueOf(sysId) + "记录不存在");
            return result;
        }
        eventNo = main.getEventNo();

        Map<String, Object> dataParts = new HashMap<>();
        dataParts.put("BaqsjRecordProperty", SetGetUtil.getField(dataRequest, "BaqsjRecordProperty"));
        dataParts.put("BaqsjRecordReport", SetGetUtil.getField(dataRequest, "BaqsjRecordReport"));
        dataParts.put("BaqsjRecordHandle", SetGetUtil.getField(dataRequest, "BaqsjRecordHandle"));

        Map<String, Class<?>> classMap = new HashMap<>();
        classMap.put("BaqsjRecordProperty", BaqsjRecordProperty.class);
        classMap.put("BaqsjRecordReport", BaqsjRecordReport.class);
        classMap.put("BaqsjRecordHandle", BaqsjRecordHandle.class);

        String busiKey = "BaqsjRecord" + eventNo;
        int eventID = BaqsjEventCOMMON.getEventID(eventNo);

        switch (eventID) {
            case BaqsjCOMMON.EVENT_ID_AccidentChokingFood: {
                UnsafeeventAccidentChokingFoodResponse response = new UnsafeeventAccidentChokingFoodResponse();
                BeanUtils.copyProperties(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordAccident buziData = baqsjRecordAccidentDao.queryByEventId(main.getId());
                BaqsjRecordAccidentChokingFood eventData = new BaqsjRecordAccidentChokingFood();
                BeanUtils.copyProperties(buziData, eventData);
                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordChokingFoodAccident(eventData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));

                break;
            }
            case BaqsjCOMMON.EVENT_ID_AccidentFall: {
                UnsafeeventAccidentFallResponse response = new UnsafeeventAccidentFallResponse();
                BeanUtils.copyProperties(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordAccident buziData = baqsjRecordAccidentDao.queryByEventId(main.getId());
                BaqsjRecordAccidentFall eventData = new BaqsjRecordAccidentFall();
                BeanUtils.copyProperties(buziData, eventData);
                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordAccidentFall(eventData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));

                break;
            }
            case BaqsjCOMMON.EVENT_ID_AccidentHurt: {
                UnsafeeventAccidentHurtResponse response = new UnsafeeventAccidentHurtResponse();
                BeanUtils.copyProperties(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordAccident buziData = baqsjRecordAccidentDao.queryByEventId(main.getId());
                BaqsjRecordAccidentHurt eventData = new BaqsjRecordAccidentHurt();
                BeanUtils.copyProperties(buziData, eventData);

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordAccidentHurt(eventData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));

                break;
            }
            case BaqsjCOMMON.EVENT_ID_AccidentOther: {
                UnsafeeventAccidentOtherResponse response = new UnsafeeventAccidentOtherResponse();
                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordAccident buziData = baqsjRecordAccidentDao.queryByEventId(main.getId());
                BaqsjRecordAccidentOther eventData = new BaqsjRecordAccidentOther();
                BeanUtils.copyProperties(buziData, eventData);

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordAccidentOther(eventData);

                page.setTotal(1);

                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_AccidentOut: {
                UnsafeeventAccidentOutResponse response = new UnsafeeventAccidentOutResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordAccident buziData = baqsjRecordAccidentDao.queryByEventId(main.getId());
                BaqsjRecordAccidentOut eventData = new BaqsjRecordAccidentOut();
                BeanUtils.copyProperties(buziData, eventData);

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordAccidentOut(eventData);

                page.setTotal(1);

                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_AccidentSelfKill: {
                UnsafeeventAccidentSelfKillResponse response = new UnsafeeventAccidentSelfKillResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordAccident buziData = baqsjRecordAccidentDao.queryByEventId(main.getId());
                BaqsjRecordAccidentSelfKill eventData = new BaqsjRecordAccidentSelfKill();
                BeanUtils.copyProperties(buziData, eventData);

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordAccidentSelfKill(eventData);

                page.setTotal(1);

                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_DiagnosisDietWrong: {
                Object response = processStandardEvent(dataRequest, requestPojo, eventClassListBoList, main, eventID, dataParts, classMap);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_DiagnosisInfusionReaction: {
                Object response = processStandardEvent(dataRequest, requestPojo, eventClassListBoList, main, eventID, dataParts, classMap);

                page.setTotal(1);

                result.setResData(page.setRecords(Arrays.asList(response)));

                break;
            }
            case BaqsjCOMMON.EVENT_ID_DiagnosisMedicalHandle: {
                Object response = processStandardEvent(dataRequest, requestPojo, eventClassListBoList, main, eventID, dataParts, classMap);

                page.setTotal(1);

                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_DiagnosisOtherReaction: {
                Object response = processStandardEvent(dataRequest, requestPojo, eventClassListBoList, main, eventID, dataParts, classMap);

                page.setTotal(1);

                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_DiagnosisSampleWrong: {
                Object response = processStandardEvent(dataRequest, requestPojo, eventClassListBoList, main, eventID, dataParts, classMap);

                page.setTotal(1);

                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_DoctorAndPatientNegotiation: {
                UnsafeeventDoctorAndPatientNegotiationResponse response = new UnsafeeventDoctorAndPatientNegotiationResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordNegotiation buziData = baqsjRecordNegotiationDao.queryByEventId(main.getId());

                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordNegotiation(buziData);

                page.setTotal(1);

                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_HospitalInfected: {
                Object response = processStandardEvent(dataRequest, requestPojo, eventClassListBoList, main, eventID, dataParts, classMap);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_MedicalBadReaction: {
                UnsafeeventMedicalBadReactionResponse response = new UnsafeeventMedicalBadReactionResponse();


                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordUntowardeffect buziData = baqsjRecordUntowardeffectDao.queryByEventId(main.getId());

                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordUntowardeffect(buziData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_MedicalWrongDrug: {
                UnsafeeventMedicalWrongDrugResponse response = new UnsafeeventMedicalWrongDrugResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordWrongdrug buziData = baqsjRecordWrongdrugDao.queryByEventId(main.getId());

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordWrongdrug(buziData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_MedicalInstrument: {
                UnsafeeventMedicalInstrumentResponse response = new UnsafeeventMedicalInstrumentResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordMedicalinstrument buziData = baqsjRecordMedicalinstrumentDao.queryByEventId(main.getId());

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordMedicalinstrument(buziData);

                page.setTotal(1);

                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_OtherReactionGeneral: {
                Object response = processStandardEvent(dataRequest, requestPojo, eventClassListBoList, main, eventID, dataParts, classMap);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_OtherReactionPublic: {
                UnsafeeventOtherReactionPublicResponse response = new UnsafeeventOtherReactionPublicResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordOther buziData = baqsjRecordOtherDao.queryByEventId(main.getId());
                BaqsjRecordOtherPublic eventData = new BaqsjRecordOtherPublic();
                BeanUtils.copyProperties(buziData, eventData);

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordOtherPublic(eventData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_OtherReactionPublicSecurity: {
                UnsafeeventOtherReactionPublicSecurityResponse response = new UnsafeeventOtherReactionPublicSecurityResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordOther buziData = baqsjRecordOtherDao.queryByEventId(main.getId());
                BaqsjRecordOtherPublicSecurity eventData = new BaqsjRecordOtherPublicSecurity();
                BeanUtils.copyProperties(buziData, eventData);

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordOtherPublicSecurity(eventData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_PreventionAndProtection: {
                UnsafeeventPreventionAndProtectionResponse response = new UnsafeeventPreventionAndProtectionResponse();
                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordProtection buziData = baqsjRecordProtectionDao.queryByEventId(main.getId());

                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordProtection(buziData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_SurgicalBad: {
                UnsafeeventSurgicalBadResponse response = new UnsafeeventSurgicalBadResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordOperation buziData = baqsjRecordOperationDao.queryByEventId(main.getId());
                BaqsjRecordSurgicalBad eventData = new BaqsjRecordSurgicalBad();
                BeanUtils.copyProperties(buziData, eventData);

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordSurgicalBad(eventData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_SurgicalNonPlaned: {
                UnsafeeventSurgicalNonPlanedResponse response = new UnsafeeventSurgicalNonPlanedResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordOperation buziData = baqsjRecordOperationDao.queryByEventId(main.getId());
                BaqsjRecordNonPlaned eventData = new BaqsjRecordNonPlaned();
                BeanUtils.copyProperties(buziData, eventData);

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordNonPlaned(eventData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_SurgicalOperationBad: {
                UnsafeeventSurgicalOperationBadResponse response = new UnsafeeventSurgicalOperationBadResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordOperation buziData = baqsjRecordOperationDao.queryByEventId(main.getId());
                BaqsjRecordOperationBad eventData = new BaqsjRecordOperationBad();
                BeanUtils.copyProperties(buziData, eventData);

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordOperationBad(eventData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_TransfusionBloodReaction: {
                UnsafeeventTransfusionBloodReactionResponse response = new UnsafeeventTransfusionBloodReactionResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordTransfusion buziData = baqsjRecordTransfusionDao.queryByEventId(main.getId());

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordTransfusion(buziData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
            case BaqsjCOMMON.EVENT_ID_TransfusionWrongBlood: {
                UnsafeeventTransfusionWrongBloodResponse response = new UnsafeeventTransfusionWrongBloodResponse();

                copyBaseProperty(main, response);

                BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
                BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
                BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
                BaqsjRecordWrongblood buziData = baqsjRecordWrongbloodDao.queryByEventId(main.getId());

                response.setBaqsjRecordProperty(property);
                response.setBaqsjRecordReport(report);
                response.setBaqsjRecordHandle(handle);
                response.setBaqsjRecordWrongblood(buziData);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }

            default: {
                Object response = processStandardEvent(dataRequest, requestPojo, eventClassListBoList, main, eventID, dataParts, classMap);

                page.setTotal(1);
                result.setResData(page.setRecords(Arrays.asList(response)));
                break;
            }
        }

        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

        return result;
    }


    /**
     * @param main
     * @param responseClass
     * @param eventDataClass
     * @param setEventData
     * @param page
     * @param result
     * @param <T>
     * @param <U>
     * @throws Exception
     */
    private <T extends Object, U extends BaqsjRecordAccident> void handleAccidentEvent(
            BaqsjRecordMain main,
            Class<T> responseClass,
            Class<U> eventDataClass,
            Consumer<T> setEventData,
            Page page,
            Result result) throws Exception {

        // 创建响应对象
        T response = responseClass.getDeclaredConstructor().newInstance();
        BeanUtils.copyProperties(main, response);

        // 查询相关数据
        BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
        BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
        BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());
        BaqsjRecordAccident buziData = baqsjRecordAccidentDao.queryByEventId(main.getId());

        // 创建事件数据对象并复制属性
        U eventData = eventDataClass.getDeclaredConstructor().newInstance();
        BeanUtils.copyProperties(buziData, eventData);

        String className = eventDataClass.getName().substring(eventDataClass.getClass().getName().lastIndexOf(".") + 1);

        // 设置响应对象的属性
        SetGetUtil.setField(response, "BaqsjRecordProperty", property);
        SetGetUtil.setField(response, "BaqsjRecordReport", report);
        SetGetUtil.setField(response, "BaqsjRecordHandle", handle);
        SetGetUtil.setField(response, className, eventData);
        setEventData.accept(response);

        // 设置分页和结果
        page.setTotal(1);
        result.setResData(page.setRecords(Arrays.asList(response)));
    }

    /**
     * 拷贝基础数据
     *
     * @param main
     * @param response
     */
    private void copyBaseProperty(BaqsjRecordMain main, Object response) {
        SetGetUtil.setField(response, "id", main.getId());
        SetGetUtil.setField(response, "auditStatus", main.getAuditStatus());
        SetGetUtil.setField(response, "eventNo", main.getEventNo());
        SetGetUtil.setField(response, "eventName", main.getEventName());
    }

    /**
     * 处理标准数据请求
     *
     * @param dataRequest
     * @param requestPojo
     * @param eventClassListBoList
     * @param main
     * @param eventID
     */
    private Object processStandardEvent(Object dataRequest, RequestPojo requestPojo, List<EventClassListBo> eventClassListBoList, BaqsjRecordMain main, int eventID, Map<String, Object> dataParts, Map<String, Class<?>> classMap) {
        UnsafeeventStandardResponse response = new UnsafeeventStandardResponse();

        copyBaseProperty(main, response);

        BaqsjRecordProperty property = baqsjRecordPropertyDao.queryByEventId(main.getId());
        BaqsjRecordReport report = baqsjRecordReportDao.queryByEventId(main.getId());
        BaqsjRecordHandle handle = baqsjRecordHandleDao.queryByEventId(main.getId());

        response.setBaqsjRecordProperty(property);
        response.setBaqsjRecordReport(report);
        response.setBaqsjRecordHandle(handle);

        return response;
    }

    /**
     * 处理各类事件，写入对应表中
     *
     * @param requestPojo          系统对象
     * @param eventClassListBoList 事件列表属性
     * @param eventID              事件ID
     * @param response             响应数据对象
     * @param data                 请求数据
     * @param dataParts            请求分类对象
     * @param classMap             类对象
     */
    @Override
    public Object processEventList(Long sysId, RequestPojo requestPojo, List<EventClassListBo> eventClassListBoList, int eventID, Object response, Object data, Map<String, Object> dataParts, Map<String, Class<?>> classMap) {
        int insertOrUpdate = 3;
        for (Map.Entry<String, Object> entry : dataParts.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            SetGetUtil.setField(value, "eventId", sysId);

            Object processedValue = process(eventClassListBoList, eventID, value, insertOrUpdate, requestPojo);
            try {
                if (Objects.nonNull(processedValue)) {
                    BeanUtils.copyProperties(processedValue, value);
                    SetGetUtil.setFieldByObject(response, key, value, classMap.get(key));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return response;
    }

    /**
     * 处理数据
     *
     * @param eventClassListBoList 事件对象
     * @param eventID              事件ID
     * @param data                 数据
     * @param operType             0-insert, 1-update, 2-delete, 其他-query
     * @return
     */
    @Override
    public Object process(List<EventClassListBo> eventClassListBoList, int eventID, Object data, int operType, RequestPojo requestPojo) {
        Object ret = null;

        if (Objects.isNull(eventClassListBoList) || Objects.isNull(data)) {
            return null; // 或者抛出异常，根据业务需求决定
        }

        EventClassListBo eventClassListBoProcess = eventClassListBoList.stream()
                .filter(bo -> bo.getEventid().equals(eventID))
                .findFirst()
                .orElse(null);

        if (Objects.isNull(eventClassListBoProcess)) {
            return null; // 或者抛出异常，根据业务需求决定
        }

        daoMap = new HashMap<>();
        daoMap.put(BaqsjRecordProperty.class, baqsjRecordPropertyDao);
        daoMap.put(BaqsjRecordReport.class, baqsjRecordReportDao);
        daoMap.put(BaqsjRecordHandle.class, baqsjRecordHandleDao);
        daoMap.put(BaqsjRecordAccident.class, baqsjRecordAccidentDao);
        daoMap.put(BaqsjRecordAccidentChokingFood.class, baqsjRecordAccidentDao);
        daoMap.put(BaqsjRecordAccidentFall.class, baqsjRecordAccidentDao);
        daoMap.put(BaqsjRecordAccidentHurt.class, baqsjRecordAccidentDao);
        daoMap.put(BaqsjRecordAccidentOther.class, baqsjRecordAccidentOtherDao);
        daoMap.put(BaqsjRecordAccidentOut.class, baqsjRecordAccidentDao);
        daoMap.put(BaqsjRecordNegotiation.class, baqsjRecordNegotiationDao);
        daoMap.put(BaqsjRecordUntowardeffect.class, baqsjRecordUntowardeffectDao);
        daoMap.put(BaqsjRecordWrongdrug.class, baqsjRecordWrongdrugDao);
        daoMap.put(BaqsjRecordMedicalinstrument.class, baqsjRecordMedicalinstrumentDao);
        daoMap.put(BaqsjRecordOtherPublic.class, baqsjRecordOtherDao);
        daoMap.put(BaqsjRecordOtherPublicSecurity.class, baqsjRecordOtherDao);
        daoMap.put(BaqsjRecordProtection.class, baqsjRecordProtectionDao);
        daoMap.put(BaqsjRecordSurgicalBad.class, baqsjRecordOperationDao);
        daoMap.put(BaqsjRecordNonPlaned.class, baqsjRecordOperationDao);
        daoMap.put(BaqsjRecordOperationBad.class, baqsjRecordOperationDao);
        daoMap.put(BaqsjRecordTransfusion.class, baqsjRecordTransfusionDao);
        daoMap.put(BaqsjRecordWrongblood.class, baqsjRecordWrongbloodDao);
        daoMap.put(BaqsjRecordInputblood.class, baqsjRecordInputbloodDao);

        String className = data.getClass().getName().substring(data.getClass().getName().lastIndexOf(".") + 1);

        Class<?> clazz = this.classMap.get(className);
        if (clazz != null) {
            Object dataEntity = JSonUtil.toBean(JSonUtil.toJSONString(data), clazz);
            Object dao = this.daoMap.get(clazz);
            if (dao instanceof BaseMapper) { // 所有 DAO 都继承自 BaseMapper
                if (operType == 0) {
                    SetGetUtil.setField(dataEntity, "id", 0L);
                    SetGetUtil.setField(dataEntity, "createBy", String.valueOf(requestPojo.getSysId()));
                    SetGetUtil.setField(dataEntity, "createTime", DateUtil.parseDate(DateUtil.formatDate(new Date(), DateUtil.DATE_TIME_FORMAT), DateUtil.DATE_TIME_FORMAT));
                    SetGetUtil.setField(dataEntity, "updateBy", String.valueOf(requestPojo.getSysId()));
                    SetGetUtil.setField(dataEntity, "updateTime", DateUtil.parseDate(DateUtil.formatDate(new Date(), DateUtil.DATE_TIME_FORMAT), DateUtil.DATE_TIME_FORMAT));
                    ((BaseMapper) dao).insert(dataEntity);
                    return dataEntity;
                } else if (operType == 1) {
                    SetGetUtil.setField(dataEntity, "updateBy", String.valueOf(requestPojo.getSysId()));
                    SetGetUtil.setField(dataEntity, "updateTime", DateUtil.parseDate(DateUtil.formatDate(new Date(), DateUtil.DATE_TIME_FORMAT), DateUtil.DATE_TIME_FORMAT));
                    ((BaseMapper) dao).updateById(dataEntity);
                    return dataEntity;
                } else if (operType == 2) {
                    Long sysId = (Long) SetGetUtil.getField(dataEntity, "id");
                    ret = ((BaseMapper) dao).deleteById(sysId);
                } else {
                    Long sysId = (Long) SetGetUtil.getField(dataEntity, "id");
                    Long eventId = (Long) SetGetUtil.getField(dataEntity, "eventId");
                    if (Objects.nonNull(sysId)) {
                        ret = ((BaseMapper) dao).selectById(sysId);
                    } else if (Objects.nonNull(eventId)) {
                        //通过event_id查询对应数据
                        try {
                            ret = SetGetUtil.invokeMethodHandle(dao.getClass(), dao, "queryByEventId", clazz, eventId);
                        } catch (Throwable ex) {
                            ex.printStackTrace();
                        }
//                        ret = dao.queryByEventId(eventId);
                    }
                }
            }
        }

        return ret; // 或者抛出异常，根据业务需求决定
    }

}
